package edu.cmu.ece.ds.droidchat.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import edu.cmu.ece.ds.droidchat.DroidChatTabActivity;
import edu.cmu.ece.ds.droidchat.NetworkService;
import edu.cmu.ece.ds.droidchat.R;
import edu.cmu.ece.ds.droidchat.model.UserInfo;
import edu.cmu.ece.ds.droidchat.widget.ActionItem;
import edu.cmu.ece.ds.droidchat.widget.QuickActionList;
import edu.cmu.ece.ds.droidchat.widget.QuickActionList.OnListClickListener;

public class LoggedInUserView extends LinearLayout {
    private ImageView userIconView;
    private TextView userName;
    private TextView userStatus;
    private ImageButton locationButton;

    private class OnLocationListClickListener implements OnListClickListener {

        @Override
        public void onQuickActionListClicked(View contentView,
                int position) {
            NetworkService mService = ((DroidChatTabActivity) (LoggedInUserView.this
                    .getContext())).getService();
            if (mService != null)
                switch (position) {
                case 0:
                    Log.d("location report mode=", "CMU Mode");
                    mService.setLocationReportMode(NetworkService.CMU_MODE);
                    break;
                case 1:
                    Log.d("location report mode=", "Waterfront Mode");
                    mService.setLocationReportMode(NetworkService.WATERFRONT_MODE);
                    break;
                case 2:
                    Log.d("location report mode=", "Real Mode");
                    mService.setLocationReportMode(NetworkService.REAL_MODE);
                    break;
                }
        }
    }

    public LoggedInUserView(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.logged_in_user, this);
        userIconView = (ImageView) view.findViewById(R.id.logged_in_user_image);
        userName = (TextView) view.findViewById(R.id.logged_in_user_nickname);
        userStatus = (TextView) view.findViewById(R.id.logged_in_user_status);

        locationButton = (ImageButton) view.findViewById(R.id.button_location);

        final QuickActionList l = new QuickActionList(
                locationButton, LinearLayout.VERTICAL);

        l.addActionItem(new ActionItem(LoggedInUserView.this.getContext(), 0,
                R.string.location_cmu, LinearLayout.HORIZONTAL));
        l.addActionItem(new ActionItem(LoggedInUserView.this.getContext(), 0,
                R.string.location_waterfront, LinearLayout.HORIZONTAL));
        l.addActionItem(new ActionItem(LoggedInUserView.this.getContext(), 0,
                R.string.location_real, LinearLayout.HORIZONTAL));
        l.setOnListClickListener(new OnLocationListClickListener());
        locationButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                l.show();
            }
        });
    }

    public ImageView getUserIconView() {
        return userIconView;
    }

    public void setUserIconView(ImageView v) {
        userIconView = v;
    }

    public void setUserIcon(Drawable userIcon) {
        this.userIconView.setImageDrawable(userIcon);
    }

    public String getUserName() {
        return userName.getText().toString();
    }

    public void setUserName(String userName) {
        this.userName.setText(userName);
    }

    public String getUserStatus() {
        return userStatus.getText().toString();
    }

    public void setUserStatus(String userStatus) {
        this.userStatus.setText(userStatus);
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userName.setText(userInfo.getNickName());
        this.userStatus.setText(userInfo.getStatus());
    }
}
